<template>
  <div>
    <el-card class="box-card">
      <!-- 头部 -->
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-row class="jiaose"> <el-button type="primary" @click="dialogVisible=true, isEdit = false">增加角色</el-button></el-row>

          <el-table
            class="jiaose"
            :data="tableData"
            border
            style="width: 100%"
          >
            <el-table-column
              fixed
              type="index"
              prop="date"
              label="序号"
              width="100"
            />
            <el-table-column
              prop="name"
              label="角色名"
              width="256"
            />

            <el-table-column
              prop="description"
              label="描述"
              width="500"
            />

            <el-table-column

              label="操作"
              width="527"
            >
              <template slot-scope="scope">
                <el-button type="success" @click="settingpermissions=true,id=scope.row.id">分配权限</el-button>
                <el-button type="primary" @click="getRolesFn(scope.row.id)">编辑</el-button>
                <el-button type="danger" @click="delRolesFn(scope.row.id)">删除</el-button>
              </template>

            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <el-pagination
            :current-page="pagesize"
            :page-size="10"
            :pager-count="11"
            :page-sizes="[2,3,4,5,6,7]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="Number(total)"
            center
            @current-change="sdFn"
            @size-change="sizeChangeFn"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息">

          <el-alert
            class="wang"
            title="消息提示的文案"
            type="info"
            show-icon
          />
          <el-form ref="form" disabled :model="form" label-width="80px" class="xinxi">
            <el-form-item label="公司名称">
              <el-input v-model="form.name" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="form.companyAddress" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="form.mailbox" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="form.remarks" type="textarea" />
            </el-form-item>

          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹框 -->
    <el-dialog
      v-if="dialogVisible"
      :title="isEdit===true?'编辑角色':'新增角色' "
      :visible.sync="dialogVisible"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="ruleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cloeFn">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      v-if="settingpermissions"
      title="权限"
      :visible.sync="settingpermissions"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center
    >
      <assign-permission :role-id="id" @uplie="uplieFn" @close="settingpermissions=false" />
    </el-dialog>
  </div>
</template>

<script>
// import setting from './setting.vue'
import { AllRolesAPI, addRolesAPI, companyAPI, getRolesAPI, EditRolesAPI, delRolesAPI } from '@/api/settingAPI'
import { mapGetters } from 'vuex'
import AssignPermission from './assignPermission.vue'
export default {
  name: 'Setting',
  components: {
    AssignPermission
  },
  data() {
    return {
      id: '',
      settingpermissions: false, // 分配权限弹窗
      tableData: [],
      total: '', // 全部数据数量
      page: 1, // 分页
      pagesize: 2, // 每页条数
      form: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''

      },
      ruleForm: { // 弹窗编辑/新增绑定
        name: '都给加上',
        description: '加狗500条'
      },
      rules: { // 弹窗编辑/新增验证
        name: [
          { required: true, message: '请输入角色', trigger: 'blur' },
          { min: 2, max: 12, message: '长度在 2 到 12 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur' }
        ]
      },
      dialogVisible: false, // 弹窗显示状态
      isEdit: false, // 编辑、新增状态
      time: null

    }
  },
  created() {
    this.AllRoles()
    this.company()
  },
  computeds: {
    ...mapGetters(['company'])
  },
  methods: {
    uplieFn() {
      this.settingpermissions = false
      this.AllRoles()
    },
    // 全部角色信息
    async  AllRoles() {
      const res = await AllRolesAPI({ page: this.page, pagesize: this.pagesize })
      this.tableData = res.data.rows
      this.total = Number(res.data.total)
      console.log(res)
    },
    // 分页
    sdFn(a) {
      this.page = a
      console.log(a)
      this.AllRoles()
    },
    // 每页显示
    sizeChangeFn(a) {
      console.log(a)
      this.pagesize = a
      this.AllRoles()
    },
    // 兜底验证、新增、、编辑角色
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          if (!this.isEdit) {
            try {
              await addRolesAPI(this.ruleForm)
              this.AllRoles()
            } catch (e) {
              this.$message('失败了')
            }
            this.dialogVisible = false
          } else {
            const res = await EditRolesAPI({
              ...this.ruleForm, ...this.company
            })
            console.log(res)
            this.AllRoles()
            this.$message.success('更新成功')
            this.dialogVisible = false
          }
        } else {
          return false
        }
      })
    },
    // 根据ID查询企业信息
    async  company() {
      const res = await companyAPI(this.company)
      this.form = res.data
      console.log(res)
    },
    // 编辑 查询角色详情
    async getRolesFn(id) {
      console.log(id)
      const res = await getRolesAPI(id)

      this.isEdit = true
      this.dialogVisible = true
      this.ruleForm = res.data
    },
    // 弹框取消按钮
    cloeFn() {
      this.dialogVisible = false
      this.ruleForm = {}
    },
    // 删除角色
    delRolesFn(id) {
      this.$confirm('此操作将永久删除此角色吗？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRolesAPI(id)
        if (this.page > 1) {
          if (this.tableData.length <= 1) {
            this.page--
          }
        }
        this.AllRoles()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
    // 分配权限弹窗

  }
}
</script>

<style lang="scss">
.app-main{
  padding:  20px;
  margin-top: 20px;
}
.box-card{
  padding: 0 30px;
}
.jiaose{
  margin: 20px;
}
.xinxi{
  width: 600px;
  padding: 0 40px;
  margin-left: 30px;
}
.wang{
  margin: 20px 0 60px 0;
}
.el-dialog-footer{
  text-align: center;
  margin: 0 auto ;
}
.el-dialog__header{
  background-color: #296eea;
  .el-dialog__title{
color: aliceblue;
  }

}
</style>
